﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using YArchitech.ProgressBar;
using YJKCodeBase.WinForm;
using YJKOpeningCommon.DataEnum;
using YJKOpeningCommon.DbOpening;
using YJKOpeningCommon.Public;

namespace YJKOpeningDesignInput.OpeningCheck
{
	[Transaction(TransactionMode.Manual)]
	[Regeneration(RegenerationOption.Manual)]
	internal class CmdOpeningCheck : IExternalCommand
	{
		public Result Execute(ExternalCommandData cmdData, ref string messages, ElementSet elements)
		{
			UIDocument activeUIDocument = cmdData.Application.ActiveUIDocument;
			Document document = activeUIDocument.Document;
			this.m_cmdData = cmdData;
			SelecteXmlPathForm selecteXmlPathForm = new SelecteXmlPathForm(ESelecteXmlPathForm.eCheckOpening);
			if (selecteXmlPathForm.ShowDialog() != DialogResult.OK)
			{
				return Autodesk.Revit.UI.Result.Cancelled;
			}
			DbOpeningImpExpManager dbOpeningImpExpManager = new DbOpeningImpExpManager(selecteXmlPathForm.XmlPathName);
			LinkedList<DbOpeningBase> lstOpening = null;
			if (!dbOpeningImpExpManager.ImportOpeningByXml(out lstOpening))
			{
				return Autodesk.Revit.UI.Result.Cancelled;
			}
			this.CreatePreviewInstancesAndShowReport(activeUIDocument, lstOpening);
			return Autodesk.Revit.UI.Result.Succeeded;
		}

		private void CreatePreviewInstancesAndShowReport(UIDocument uiDoc, LinkedList<DbOpeningBase> lstOpening)
		{
			Document document = uiDoc.Document;
			ViewScene viewScene = new ViewScene();
			viewScene.m_viewOpening2D = document.ActiveView;
			GraphicsStyle lineStyle = CommonInterface.GetLineStyle(document, "HYGreenLineStyle", new Color(0, byte.MaxValue, 0), 5);
			if (lineStyle == null)
			{
				return;
			}
			PreViewScene preViewScene = new PreViewScene(document, lineStyle.Id, 0.32808398950131235);
			if (lineStyle == null)
			{
				return;
			}
            Autodesk.Revit.DB.View activeView = uiDoc.ActiveView;
			if (CmdOpeningCheck.m_HWndRevit == null)
			{
				Process currentProcess = Process.GetCurrentProcess();
				IntPtr mainWindowHandle = currentProcess.MainWindowHandle;
				CmdOpeningCheck.m_HWndRevit = new WindowHandle(mainWindowHandle);
			}
			CommonInterface.SetView3DChanged(uiDoc);
			using (TransactionGroup transactionGroup = new TransactionGroup(document))
			{
				transactionGroup.Start("TransGroupCreateModelGroup");
				if (!CmdOpeningCheck.CreateOpeningPreviewInstanceFromLstOpening(lstOpening, preViewScene))
				{
					transactionGroup.RollBack();
				}
				else
				{
					OpeningCheckReportForm openingCheckReportForm = new OpeningCheckReportForm(uiDoc, lstOpening, CmdOpeningCheck.m_HWndRevit);
					openingCheckReportForm.Show(CmdOpeningCheck.m_HWndRevit);
					Selection selection = this.m_cmdData.Application.ActiveUIDocument.Selection;
					for (;;)
					{
						try
						{
							List<Element> selElements = new List<Element>();
							RevitVersionFuncs.SetSelection(uiDoc.Selection, selElements);
							uiDoc.RefreshActiveView();
							selection.PickElementsByRectangle("");
							continue;
						}
						catch (Exception)
						{
							if (!openingCheckReportForm.m_bFormClose)
							{
								openingCheckReportForm.Close();
							}
							transactionGroup.RollBack();
						}
						break;
					}
				}
			}
		}

		private static bool CreateOpeningPreviewInstanceFromLstOpening(LinkedList<DbOpeningBase> lstOpening, PreViewScene preViewScene)
		{
			if (lstOpening == null)
			{
				return false;
			}
			ProgressBarForm progressBarForm = new ProgressBarForm(lstOpening.Count, ProgressBarStyle.Blocks, true);
			progressBarForm.Text = "正在创建模型组...";
			progressBarForm.ShowInTaskbar = false;
			progressBarForm.Show(CmdOpeningCheck.m_HWndRevit);
			foreach (DbOpeningBase dbOpeningBase in lstOpening)
			{
				Element element = null;
				dbOpeningBase.CreateOpeningPreviewInstance(out element, preViewScene);
				if (element != null)
				{
					dbOpeningBase.m_idModelGroup = element.Id;
				}
				progressBarForm.RefreshProgressBar();
				if (progressBarForm.m_stopTag)
				{
					return false;
				}
			}
			progressBarForm.Dispose();
			return true;
		}

		public static WindowHandle m_HWndRevit;

		private ExternalCommandData m_cmdData;
	}
}
